<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

    <script>
        $(function () {
            show();
        })
        //名字模糊查询
        function show() {
            var specName = $("#selectSpecName").val();
            $.ajax({
                url:"/specification/getInfo",
                dataType:"json",
                type:"post",
                data:{specName:specName},
                success:function (result) {
                    var data=result.data;
                    var tbody="";
                    for (var i=0;i<data.length;i++){
                        tbody += '<tr>';
                        tbody += '<td><input  type="checkbox" name="box" value="'+data[i].id+'"></td>';
                        tbody += '<td>'+data[i].id+'</td>';
                        tbody += '<td>'+data[i].specName+'</td>';
                        tbody += '<td class="text-center">';
                        tbody += '<button type="button" class="btn bg-olive btn-xs" onclick="getDataById('+data[i].id+')" data-toggle="modal" data-target="#updateModal">修改</button>';
                        tbody += '</td>';
                        tbody += '</tr>';
                    }
                    $("#tbody").html(tbody);
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }
        
        //删除
        function deleteSpecification() {
            var boxs=$("[type=checkbox]:checked");
            var ids=[];
            $(boxs).each(function (i,e) {
                ids.push(e.value);
            })
            if (ids.length <0){
                return;
            }
            $.ajax({
                url:"/specification/deleteByIds",
                type:"post",
                dataType:"json",
                data:{ids:ids},
                success:function (result) {
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }


        //删除表格
        function deleteId(obj) {
            var parent = obj.parentNode.parentNode;
            parent.parentNode.removeChild(parent);
        }
        //添加表格 id,序号
        var count =0;
        //增加操作
        function insertTable() {
            //获取table的id
            var  tbody=document.getElementById("specification");
            //id值增加
            ++count;
            //创建tr标签
            var tr=document.createElement("tr");
            //设置tr样式
            tr.innerHTML = '<td id="'+count+'"><input  class="form-control" placeholder="规格选项"> </td> <td> <input  class="form-control" placeholder="排序"> </td> <td> <button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button> </td>';
            //添加tr标签
            tbody.appendChild(tr);
        }
        var trid = 0;
        //修改窗口的表格增加操作
        function updateTable(){
            //获取table的id
            var tbody = document.getElementById("updateTable");
            //id值增加
            ++trid;
            //创建tr标签
            var tr = document.createElement("tr");
            //设置tr的样式
            tr.innerHTML = '<td id="'+trid+'"><input  class="form-control" placeholder="规格选项"> </td> <td> <input  class="form-control" placeholder="排序"> </td> <td> <button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button> </td>';
            //添加tr标签
            tbody.appendChild(tr);
        }

        //增加
        function insert() {
            var optionList=[];
            $("#specification").find("tr").each(function () {
             var tdArr=$(this).children();
                var optionName = tdArr.eq(0).find("input").val();//规格选项
                var order = tdArr.eq(1).find("input").val();//排序
                var options = {"optionName":optionName,"orders":order};
                optionList.push(options);
            });
            var json=JSON.stringify(optionList);
            var specName=$("#specName").val();
            $.ajax({
                url:"/specification/add",
                type:"post",
                dataType:"json",
                data:{specName : specName,json : json},
                success:function (result) {
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert(result.message);
                }
            })
        }
        
        //回显
        function  getDataById(id) {
            $.ajax({
                url:"/specification/getDataById/"+id,
                type:"post",
                dataType:"json",
                success:function (result) {
                    $("#specId").val(result.data.id);
                    $("#spName").val(result.data.specName);
                    var tbody="";
                    var list=result.data.list;
                    for(var i=0;i<list.length;i++){
                        tbody += '<tr>';
                        tbody += '<td>';
                        tbody += '<input  class="form-control" name="optionName" value="'+list[i].optionName+'">';
                        tbody += '</td>';
                        tbody += '<td>';
                        tbody += '<input  class="form-control" name="orders" value="'+list[i].orders+'">';
                        tbody += '</td>';
                        tbody += '<td>';
                        tbody += '<button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button>';
                        tbody += '</td>';
                        tbody += '</tr>';
                    }
                    $("#updateTable").html(tbody);
                },
                error:function(result) {
                    alert(result.message);
                }
            })
        }
        
        //修改
        function updateSpec() {
            var optionList=[];
            $("#updateTable").find("tr").each(function () {
                var tdArr=$(this).children();
                var optionName = tdArr.eq(0).find("input").val();//规格选项
                var order = tdArr.eq(1).find("input").val();//排序
                var options = {"optionName":optionName,"orders":order};
                optionList.push(options);
            });
            var json=JSON.stringify(optionList);//转成字符串
            var specName=$("#spName").val();
            var id=$("#specId").val();
            $.ajax({
                url:"/specification/updateSpecification",
                type:"post",
                dataType:"json",
                data:{id : id,specName : specName,json : json},
                success:function(result){
                    alert(result.message);
                    location.reload();
                },
                error:function (result) {
                    alert.error(result.message);

                }
            })
        }
    </script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteSpecification()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input id="selectSpecName" name="specName" >
									<button class="btn btn-default" onclick="show()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    

<!-- 编辑窗口 -->
  <!-- 编辑窗口 -->
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog" >
          <div class="modal-content">
              <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h3 id="myModalLabel1">规格编辑</h3>
              </div>
              <div class="modal-body">

                  <table class="table table-bordered table-striped"  width="800px">
                      <tr>
                          <td>规格名称</td>
                          <td><input  class="form-control" placeholder="规格名称" name="specName" id="specName">  </td>
                      </tr>
                  </table>

                  <!-- 规格选项 -->
                  <div class="btn-group">
                      <button type="button" class="btn btn-default" title="新建"  onclick="insertTable()"><i class="fa fa-file-o"></i> 新增规格选项</button>

                  </div>

                  <table class="table table-bordered table-striped table-hover dataTable" id="table">
                      <thead>
                      <tr>


                          <th class="sorting">规格选项</th>
                          <th class="sorting">排序</th>
                          <th class="sorting">操作</th>
                      </thead>
                      <tbody id="specification">
                      <tr>
                          <td>
                              <input  class="form-control" placeholder="规格选项">
                          </td>
                          <td>
                              <input  class="form-control" placeholder="排序">
                          </td>
                          <td>
                              <button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button>
                          </td>
                      </tr>
                      <tr>
                          <td>
                              <input  class="form-control" placeholder="规格选项">
                          </td>
                          <td>
                              <input  class="form-control" placeholder="排序">
                          </td>
                          <td>
                              <button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button>
                          </td>
                      </tr>
                      <tr>
                          <td>
                              <input  class="form-control" placeholder="规格选项">
                          </td>
                          <td>
                              <input  class="form-control" placeholder="排序">
                          </td>
                          <td>
                              <button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button>
                          </td>
                      </tr>
                      </tbody>
                  </table>


              </div>
              <div class="modal-footer">
                  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
                  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
              </div>
          </div>
      </div>
  </div>


  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog" >
          <div class="modal-content">
              <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h3 id="myModalLabel">规格编辑</h3>
              </div>
              <div class="modal-body">

                  <table class="table table-bordered table-striped"  width="800px">
                      <tr>
                          <td><input type="hidden" name="id" id="specId"></td>
                      </tr>
                      <tr>
                          <td>规格名称</td>
                          <td><input  class="form-control" placeholder="规格名称" name="specName" id="spName">  </td>
                      </tr>
                  </table>

                  <!-- 规格选项 -->
                  <div class="btn-group">
                      <button type="button" class="btn btn-default" title="新建"  onclick="updateTable()"><i class="fa fa-file-o"></i> 新增规格选项</button>

                  </div>

                  <table class="table table-bordered table-striped table-hover dataTable">
                      <thead>
                      <tr>
                          <th class="sorting">规格选项</th>
                          <th class="sorting">排序</th>
                          <th class="sorting">操作</th>
                      </thead>
                      <tbody id="updateTable">
                      </tbody>
                  </table>


              </div>
              <div class="modal-footer">
                  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateSpec()">保存</button>
                  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
              </div>
          </div>
      </div>
  </div>

</body>

</html>